package com.dianban.utils;

import org.apache.commons.lang.StringUtils;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 推荐码生成工具
 */
public class RecommendCodeUtil {

    /**
     * 推荐码生成
     * 随机3个数字+四个字母+1位数字+1个字母组合
     *
     * @return
     */
    public static String generateRecommendCode() {
        String str = "";
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            str += String.valueOf(random.nextInt(10));
        }
        for (int i = 0; i < 4; i++) {
            str += (char) (65 + random.nextInt(26));// 取得大写字母
        }
        for (int i = 0; i < 1; i++) {
            str += String.valueOf(random.nextInt(10));
        }
        for (int i = 0; i < 1; i++) {
            str += (char) (65 + random.nextInt(26));// 取得大写字母
        }
        return str;
    }

    /**
     * 认领码
     * 随机3个数字+2个字母+1位数字组合
     *
     * @return
     */
    public static String generateActivationCode() {
        String str = "";
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            str += String.valueOf(random.nextInt(10));
        }
        for (int i = 0; i < 2; i++) {
            str += (char) (65 + random.nextInt(26));// 取得大写字母
        }
        for (int i = 0; i < 1; i++) {
            str += String.valueOf(random.nextInt(10));
        }
        return str;
    }

    /**
     * 生成随机数
     *
     * @param length
     * @return
     */
    public static String randomNumbers(int length) {
        return randomString("0123456789", length);
    }

    public static String randomString(String baseString, int length) {
        if (StringUtils.isEmpty(baseString)) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder(length);
            if (length < 1) {
                length = 1;
            }

            int baseLength = baseString.length();

            for (int i = 0; i < length; ++i) {
                int number = getRandom().nextInt(baseLength);
                sb.append(baseString.charAt(number));
            }

            return sb.toString();
        }
    }

    public static ThreadLocalRandom getRandom() {
        return ThreadLocalRandom.current();
    }
}
